﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMKB_Repository;
using PMKB_DAL;

namespace PMKB_Web.Controllers
{
    public partial class UserController : Controller
    {
        //
        // GET: /User/
        public virtual ActionResult Index(int? pageNumber, int? pageSize, string Search)
        {
            SearchBase s = new SearchBase();
            s.Search = Search;
            s.PageSize = pageSize;
            s.PageNumber = pageNumber;
            s.DefaultValues();
            return Index(s);
        }
        private ActionResult Index(SearchBase s)
        {
            UserLoad ul = new UserLoad();
            ul.Connection = SiteUtils.ConnectionString;
            var data=ul.LoadUsers(s);
            return View(data);
        }

        //
        // GET: /User/Details/5

        public virtual ActionResult Details(long id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public virtual ActionResult Create()
        {
            
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public virtual ActionResult Create(User u)
        {
            try
            {
                UserLoad ul = new UserLoad();
                ul.Connection = SiteUtils.ConnectionString;
                ul.CreateUser(u);
                return RedirectToAction(MVC.User.Index());
            }
            catch(Exception ex)
            {
                var errormsg = string.Format(Resources.Resources.ErrorCreateUser, u.UserName, ex.Message);
                ModelState.AddModelError("", errormsg);
                return View(u);
            }
        }
        
        //
        // GET: /User/Edit/5

        public virtual ActionResult Edit(long id)
        {
            UserLoad ul = new UserLoad();
            ul.Connection = SiteUtils.ConnectionString;
            var user=ul.LoadUser(id);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public virtual ActionResult Edit(User u)
        {
            try
            {

                UserLoad ul = new UserLoad();
                ul.Connection = SiteUtils.ConnectionString;
                ul.SaveUser(u);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
        [HttpPost]
        public virtual ActionResult Delete(int id)
        {
            try
            {


                UserLoad ul = new UserLoad();
                ul.Connection = SiteUtils.ConnectionString;                
                ul.Delete(id, common.UserFind.UserName);
                return Json(new { ok = true, message = "" });

            }
            catch (Exception ex)
            {
                //TODO: log
                var errormsg = string.Format(Resources.Resources.ErrorDel, ex.Message);
                return Json(new { ok = false, errormsg });
            }
        }

    }
}
